package de.jonasblatt.networkapi.rpi.model;

import java.awt.AWTException;
import java.io.IOException;

import com.pi4j.system.SystemInfo;

import de.jonasblatt.networkapi.rpi.model.gpio.GpioWorker;
import de.jonasblatt.networkapi.rpi.model.gpio.LedBlinker;

public class RpiWorker implements NetworkModel {
	private static RpiWorker instance;

	public static RpiWorker getInstance() throws AWTException {
		if (RpiWorker.instance == null) {
			RpiWorker.instance = new RpiWorker();
		}
		return RpiWorker.instance;
	}

	private int counter;
	private GpioWorker gpioworker;

	public GpioWorker getGpioworker() {
		return this.gpioworker;
	}

	private RpiWorker() {
		this.counter = 0;
		this.gpioworker = new GpioWorker();
	}

	public void blinkLed(String gpio, int times, long wait) {
		new Thread(new LedBlinker(this.gpioworker, gpio, times, wait)).start();
	}

	public String getSystemVal(String val) {
		try {
			switch (val) {
			case "temp":
				return Float.toString(SystemInfo.getCpuTemperature());
			default:
				return "System val " + val + "not implemented.";
			}
		} catch (NumberFormatException | IOException | InterruptedException e) {
			return "";
		}
	}

	public String getTemp() {
		return this.getSystemVal("temp");
	}

	public int sayHello(String name) {
		System.out.println("Hello world and hello " + name);
		this.counter++;
		return this.counter;
	}

}
